package com.tang.Web;

import com.sun.org.apache.xpath.internal.operations.Or;
import com.tang.Dao.UserDao;
import com.tang.Utils.JDBCUtils;
import com.tang.pojo.Cart;
import com.tang.pojo.Order;
import com.tang.pojo.OrderItem;
import com.tang.service.OrderServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
public class OrderServlet extends BaseServlet{
    protected void generateOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException {
        resp.setContentType("text/html;charset=UTF-8");
        UserDao ud=new UserDao();
        Cart cart=(Cart)req.getSession().getAttribute("cart");
        String name=(String)req.getSession().getAttribute("userName");
        int id=ud.queryByName(name).getId();
        OrderServiceImpl osi=new OrderServiceImpl();
        int orderID = osi.createOrder(cart, id);
        System.out.println("生成订单成功!");
        req.setAttribute("orderId",orderID);
        req.getRequestDispatcher("pages/orderGenerate.jsp").forward(req,resp);
    }
    protected void showOrders(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
        OrderServiceImpl osi=new OrderServiceImpl();
        List<Order> lists=osi.showAllOrders();
        req.setAttribute("orders",lists);
        req.getRequestDispatcher("pages/Manage/OrderManager.jsp").forward(req,resp);
    }
    protected void sendAOrder(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
        String id=req.getParameter("id");
        int idInt=Integer.parseInt(id);
        OrderServiceImpl osi=new OrderServiceImpl();
        osi.sendStock(idInt);
        System.out.println("该订单已经成功发货!");
        showOrders(req,resp);
    }
    protected void showOrderDetails(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
        String id=req.getParameter("id");
        int orderID=Integer.parseInt(id);
        OrderServiceImpl osi=new OrderServiceImpl();
        List<OrderItem> item=osi.showDetails(orderID);
        req.setAttribute("details",item);
        req.getRequestDispatcher("pages/Manage/OrderDetails.jsp").forward(req,resp);
    }
    protected void showMyAllOrders(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
        String name=req.getParameter("userName");
        UserDao ud=new UserDao();
        int userID=ud.queryByName(name).getId();
        OrderServiceImpl osi=new OrderServiceImpl();
        List<Order> orders=osi.showMyOrders(userID);
        req.setAttribute("orders",orders);
        req.getRequestDispatcher("pages/MyOrders.jsp").forward(req,resp);
    }
    protected void showMyOrderDetails(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
        String id=req.getParameter("id");
        int orderID=Integer.parseInt(id);
        OrderServiceImpl osi=new OrderServiceImpl();
        List<OrderItem> item=osi.showDetails(orderID);
        req.setAttribute("details",item);
        req.getRequestDispatcher("pages/MyOrdersDetail.jsp").forward(req,resp);
    }
    protected void receiveOrder(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
        String id=req.getParameter("id");
        int idInt=Integer.parseInt(id);
        OrderServiceImpl osi=new OrderServiceImpl();
        osi.receiveBook(idInt);
        System.out.println("该订单已经成功签收!");
        List<Order> lists=osi.showAllOrders();
        req.setAttribute("orders",lists);
        req.getRequestDispatcher("pages/MyOrders.jsp").forward(req,resp);
    }
}
